package com.workspace.chb_website.controller;

import com.workspace.chb_website.bean.RespBean;
import com.workspace.chb_website.bean.MetaBean;
import com.workspace.chb_website.bean.User;
import com.workspace.chb_website.service.WorkLogService;
import com.workspace.chb_website.util.Util;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.util.*;

@Transactional
@CrossOrigin(origins = "*")
@RestController
@Api(tags = "工作记录操作相关接口")
public class WorkLogController {

    private final WorkLogService workLogService;

    @Autowired
    public WorkLogController(WorkLogService workLogService) {
        this.workLogService = workLogService;
    }

    @ApiOperation("获取所有项目名字")
    @GetMapping("/getAllProject")
    public RespBean getAllProject() {
        return new RespBean(workLogService.getAllProject(), new MetaBean(200, "请求成功"));
    }

    @ApiOperation("模糊查找项目名字")
    @ApiImplicitParams({@ApiImplicitParam(name = "para", value = "关键字", required = true, dataType = "String")})
    @GetMapping("/findProject")
    public RespBean findProject(@RequestParam String para) {
        return new RespBean(workLogService.findProject(para), new MetaBean(200, "请求成功!"));
    }

    @ApiOperation("查找当前用户所有工作记录中最早的时间和最晚的时间")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String")})
    @GetMapping("/getWorkDate")
    public RespBean getWorkDate(@RequestParam String name) {
        return new RespBean(workLogService.getWorkDate(name), new MetaBean(200, "请求成功!"));
    }

    @ApiOperation("查找当前用户某个月的所有项目名字和起止时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String")})
    @GetMapping("/getWorkLog")
    public RespBean getWorkLog(@RequestParam String name, @RequestParam String month) {
        List<Map<Object, Object>> re = workLogService.getWorkLog(name, month);
        return new RespBean(re, new MetaBean(200, "请求成功"));
    }

    @ApiOperation("返回当前用户所有工作记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "String")})
    @GetMapping("/getAllWorkLog")
    public RespBean getAllWorkLog(@RequestParam String month){
        User currentUser = Util.getCurrentUser();
        List<Map<String, String>> re = workLogService.getAllWorkLog(currentUser, month);
        return new RespBean(re, new MetaBean(200, "请求成功"));
    }

    @ApiOperation("添加工作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enddate", value = "结束日期", required = true, dataType = "String"),
            @ApiImplicitParam(name = "startdate", value = "起始日期", required = true, dataType = "String"),
            @ApiImplicitParam(name = "project", value = "项目名字", required = true, dataType = "String")})
    @PostMapping("/addWorkLog")
    public RespBean addWorkLog(@ApiIgnore @RequestBody Map<String, String> params) throws ParseException {
        User currentUser = Util.getCurrentUser();
        return workLogService.addWorkLog(currentUser,params.get("project"),params.get("startdate"), params.get("enddate"));
    }

    @ApiOperation("删除工作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enddate", value = "结束日期", required = true, dataType = "String"),
            @ApiImplicitParam(name = "startdate", value = "起始日期", required = true, dataType = "String")})
    @PostMapping("/deleteWorkLog")
    public RespBean deleteWorkLog(@ApiIgnore @RequestBody Map<String, String> params) throws ParseException {
        User currentUser = Util.getCurrentUser();
        return workLogService.deleteWorkLog(currentUser,params.get("startdate"), params.get("enddate"));
    }
}
